import React from 'react'
import { Button, Menu, Dropdown, message, Space } from 'antd'
import { DownOutlined } from '@ant-design/icons'
import { setDataFromRap2, initData, setDataFromJSON } from '../../api/welcome'

function Welcome() {
    // 数据导入按钮点击事件
    const addDataImport = async (key) => {
        message.loading('数据加载中', 0)
        if (key.key === '1') {
            const res = await setDataFromRap2()
            // console.log(res)
            if (res.code === 0) {
                message.destroy()
                message.success('数据导入成功', 3)
            }
        } else if (key.key === '2') {
            const res = await setDataFromJSON()
            if (res.code === 0) {
                message.destroy()
                message.success('数据导入成功', 3)
            }
        }
    }
    // 清除数据按钮点击事件
    const initButtonEvent = async () => {
        message.loading('数据清除中', 0)
        const res = await initData()
        if (res.code === 0) {
            message.destroy()
            message.success('数据清除成功', 3)
        }
    }
    const menu = (
        <Menu onClick={addDataImport}>
            <Menu.Item key="1">随机数据导入</Menu.Item>
            <Menu.Item key="2">真实数据导入</Menu.Item>
        </Menu>
    )
    return (
        <>
            <div>欢迎页</div>
            <Space>
                <Dropdown overlay={menu}>
                    <Button type="primary">
                        数据导入覆盖 <DownOutlined />
                    </Button>
                </Dropdown>
                <Button onClick={initButtonEvent}>清除数据</Button>
            </Space>
        </>
    )
}

export default Welcome
